using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web.Admin;
using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_JXC_AccountSubject : Admin, IRequiresSessionState
{
	protected ScriptManager sm;
	protected HtmlTable table_item;
	protected TreeView TVAccountSubject;
	protected Label lblASTitle;
	protected HtmlGenericControl divAccountSubject;
	protected PlaceHolder phManage;
	protected Literal ltrTitle;
	protected Label lblParentName;
	protected HtmlAnchor updateParent;
	protected TextBox txtAccountSNumber;
	protected TextBox txtAccountSName;
	protected TextBox txtAccountSSpell;
	protected DropDownList ddlAccountSLend;
	protected DropDownList ddlAccountSAuxiliaryCheck;
	protected TextBox txtRemark;
	protected HiddenField hfAccountSRootID;
	protected HiddenField hfAccountSDepth;
	protected HiddenField hfAccountSParentID;
	protected Button btnAdd;
	protected HtmlTable AddTable;
	protected PlaceHolder phAddOrEdit;
	protected PlaceHolder phCheck;
	private string linkhead = "";
	private string beforeurl = "Admin_JXC_AccountSubject.aspx?MenuName=进销存管理&FileName=会计科目管理";
	private string actionTab = "";
	private int infoID = 0;
	private int aSParentID = 0;
	private HoWave.Data.Bll.AccountSubject bllAccountSubject;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public string ActionTab
	{
		get
		{
			if (base.Request.QueryString["action"] != null)
			{
				this.actionTab = base.Request.QueryString["action"].Trim();
			}
			else
			{
				this.actionTab = "";
			}
			return this.actionTab;
		}
		set
		{
			this.actionTab = value;
		}
	}
	public int InfoID
	{
		get
		{
			if (base.Request.QueryString["InfoID"] != null)
			{
				this.infoID = Convert.ToInt32(base.Request.QueryString["InfoID"].Trim());
			}
			return this.infoID;
		}
	}
	public int ASParentID
	{
		get
		{
			if (base.Request.QueryString["ASParentID"] != null)
			{
				this.aSParentID = Convert.ToInt32(base.Request.QueryString["ASParentID"].Trim());
				this.linkhead = this.linkhead + "&ASParentID=" + this.aSParentID;
			}
			else
			{
				this.aSParentID = 0;
				this.linkhead += "&ASParentID=0";
			}
			return this.aSParentID;
		}
	}
	public HoWave.Data.Bll.AccountSubject BllAccountSubject
	{
		get
		{
			if (this.bllAccountSubject == null)
			{
				this.bllAccountSubject = new HoWave.Data.Bll.AccountSubject();
			}
			return this.bllAccountSubject;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.admin.CheckAdminPower(46))
			{
				base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Admin_JXC_AccountSubject");
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(this.ActionTab);
			this.txtAccountSNumber.Enabled = false;
			if (this.InfoID > 0)
			{
				this.InitPage();
			}
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "addinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.phCheck.Visible = false;
				this.table_item.Visible = false;
				this.ltrTitle.Text = "添加会计科目";
				this.btnAdd.Text = "添加";
				this.GetRootIDOrDepthByParentID();
				this.hfAccountSParentID.Value = "0";
				this.lblParentName.Text = "顶级科目";
				return;
			}
			if (DisplayName == "addinfoChild")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.phCheck.Visible = false;
				this.table_item.Visible = false;
				this.ltrTitle.Text = "添加子科目";
				this.btnAdd.Text = "添加";
				return;
			}
			if (DisplayName == "editinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = true;
				this.phCheck.Visible = false;
				this.table_item.Visible = false;
				this.ltrTitle.Text = "修改会计科目";
				this.btnAdd.Text = "修改";
				return;
			}
			if (DisplayName == "checkinfo")
			{
				this.phManage.Visible = false;
				this.phAddOrEdit.Visible = false;
				this.phCheck.Visible = true;
				this.table_item.Visible = false;
				this.btnAdd.Text = "查看会计科目";
				return;
			}
		}
		this.phManage.Visible = true;
		this.phAddOrEdit.Visible = false;
		this.phCheck.Visible = false;
		this.table_item.Visible = true;
		this.TreeViewClassBind();
	}
	protected void TreeViewClassBind()
	{
		this.TVAccountSubject.SelectedNodeStyle.BackColor = Color.Black;
		this.TVAccountSubject.Nodes.Clear();
		TreeNode treeNode = new TreeNode();
		treeNode.Text = "<a  style=\"cursor:pointer\" id='node0' onClick=\"GetAttribute('会计科目','0')\">会计科目</a>";
		treeNode.Value = "0";
		this.TVAccountSubject.Nodes.Add(treeNode);
		this.ChildNode(treeNode, treeNode.Value);
	}
	protected void ChildNode(TreeNode node, string fid)
	{
		if (this.ASParentID == Convert.ToInt32(fid))
		{
			node.Selected = true;
			this.TVAccountSubject.SelectedNodeStyle.BackColor = Color.Silver;
			this.TVAccountSubject.ExpandAll();
		}
		DataTable dataTable = this.BllAccountSubject.GetList("AccountSParentID=" + fid).Tables[0];
		if (dataTable.Rows.Count != 0)
		{
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				TreeNode treeNode = new TreeNode();
				string text = dataTable.Rows[i]["AccountSName"].ToString();
				string text2 = dataTable.Rows[i]["AccountSubjectID"].ToString();
				string text3 = string.Concat(new string[]
				{
					"<a style=\"cursor:pointer\" id='node",
					text2,
					"' onClick=\"GetAttribute('",
					text,
					"','",
					text2,
					"')\">",
					text,
					"</a>"
				});
				treeNode.Text = text3;
				treeNode.Value = text2;
				node.ChildNodes.Add(treeNode);
				this.ChildNode(treeNode, treeNode.Value);
			}
		}
	}
	protected void InitPage()
	{
		HoWave.Data.Model.AccountSubject model = this.BllAccountSubject.GetModel(this.InfoID);
		if (model != null)
		{
			string text = this.ActionTab;
			if (text != null)
			{
				if (!(text == "editinfo"))
				{
					if (!(text == "addinfoChild"))
					{
						if (!(text == "checkinfo"))
						{
						}
					}
					else
					{
						this.hfAccountSParentID.Value = model.AccountSubjectID.ToString();
						this.hfAccountSRootID.Value = model.AccountSRootID.ToString();
						this.hfAccountSDepth.Value = (Convert.ToInt32(model.AccountSDepth) + 1).ToString();
						DataTable dataTable = this.BllAccountSubject.GetList("AccountSParentID=" + model.AccountSubjectID + " order by AccountSRootID desc").Tables[0];
						this.lblParentName.Text = this.BllAccountSubject.Display_ParentName(model.AccountSubjectID);
						if (dataTable.Rows.Count > 0)
						{
							int num = dataTable.Rows.Count + 10;
							string text2 = model.AccountSNumber + num;
							bool flag = false;
							for (int i = 0; i < dataTable.Rows.Count; i++)
							{
								string a = dataTable.Rows[i]["AccountSNumber"].ToString();
								if (a == text2)
								{
									flag = true;
									break;
								}
							}
							if (flag)
							{
								text2 = "";
								ArrayList arrayList = new ArrayList();
								for (int i = 0; i < dataTable.Rows.Count; i++)
								{
									string value = dataTable.Rows[i]["AccountSNumber"].ToString();
									arrayList.Add(value);
								}
								ArrayList arrayList2 = new ArrayList();
								for (int j = 10; j < num; j++)
								{
									string value2 = (model.AccountSNumber + j).ToString();
									arrayList2.Add(value2);
								}
								for (int i = 0; i < arrayList.Count; i++)
								{
									for (int j = 0; j < arrayList2.Count; j++)
									{
										string a2 = arrayList[i].ToString();
										string text3 = arrayList2[j].ToString();
										if (a2 == text3)
										{
											arrayList2.Remove(text3);
											break;
										}
									}
								}
								if (arrayList2.Count > 0)
								{
									text2 = arrayList2[0].ToString();
								}
							}
							this.txtAccountSNumber.Text = text2;
						}
						else
						{
							this.txtAccountSNumber.Text = model.AccountSNumber + "10";
						}
					}
				}
				else
				{
					this.hfAccountSParentID.Value = model.AccountSParentID.ToString();
					if (model.AccountSParentID.ToString() != "0")
					{
						int id = Convert.ToInt32(model.AccountSParentID.ToString());
						this.lblParentName.Text = this.BllAccountSubject.Display_ParentName(id);
					}
					else
					{
						this.lblParentName.Text = "顶级科目";
					}
					this.hfAccountSRootID.Value = model.AccountSRootID.ToString();
					this.hfAccountSDepth.Value = model.AccountSDepth.ToString();
					this.txtAccountSNumber.Text = model.AccountSNumber;
					this.txtAccountSName.Text = model.AccountSName;
					this.txtAccountSSpell.Text = model.AccountSSpell;
					this.ddlAccountSLend.ddlSelect(model.AccountSLend);
					this.ddlAccountSAuxiliaryCheck.SelectedValue = model.AccountSAuxiliaryCheck;
					this.txtRemark.Text = model.Remark;
					this.updateParent.Visible = true;
					this.updateParent.Attributes.Add("onclick", "SelectAccountSubjectParentID('" + this.hfAccountSParentID.ClientID + "');");
				}
			}
		}
	}
	private HoWave.Data.Model.AccountSubject GetInfoModel()
	{
		HoWave.Data.Model.AccountSubject accountSubject = new HoWave.Data.Model.AccountSubject();
		string value = this.hfAccountSParentID.Value;
		string text = this.txtAccountSNumber.Text;
		string text2 = this.txtAccountSName.Text;
		string text3 = this.txtAccountSSpell.Text;
		string selectedValue = this.ddlAccountSLend.SelectedValue;
		string selectedValue2 = this.ddlAccountSAuxiliaryCheck.SelectedValue;
		string text4 = this.txtRemark.Text;
		accountSubject.AccountSParentID = new int?(Convert.ToInt32(value));
		accountSubject.AccountSNumber = text;
		accountSubject.AccountSName = text2;
		accountSubject.AccountSSpell = text3;
		accountSubject.AccountSLend = selectedValue;
		accountSubject.AccountSAuxiliaryCheck = selectedValue2;
		accountSubject.Remark = text4;
		return accountSubject;
	}
	private bool CheckInput()
	{
		return true;
	}
	protected void btnAdd_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			string text = this.ActionTab;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (!(text == "addinfoChild"))
					{
						if (text == "editinfo")
						{
							this.GetRootIDOrDepthByID(Convert.ToInt32(this.hfAccountSParentID.Value));
							HoWave.Data.Model.AccountSubject accountSubject = this.BllAccountSubject.GetModel(this.InfoID);
							accountSubject = this.GetInfoModel();
							accountSubject.AccountSubjectID = this.InfoID;
							this.SetRootIDDepth(accountSubject);
							bool flag = this.BllAccountSubject.Update(accountSubject);
							if (flag)
							{
								this.WriteSuccessMsg("", "会计科目信息修改成功！", this.beforeurl);
							}
							else
							{
								this.WriteErrMsg("", "会计科目信息修改失败！", "");
							}
						}
					}
					else
					{
						HoWave.Data.Model.AccountSubject infoModel = this.GetInfoModel();
						this.SetRootIDDepth(infoModel);
						int num = this.BllAccountSubject.Add(infoModel);
						if (num > 0)
						{
							this.WriteSuccessMsg("", "子科目信息添加成功！", this.beforeurl + "&ASParentID=" + this.hfAccountSParentID.Value);
						}
						else
						{
							this.WriteErrMsg("", "子科目信息添加失败！", "");
						}
					}
				}
				else
				{
					HoWave.Data.Model.AccountSubject infoModel2 = this.GetInfoModel();
					this.SetRootIDDepth(infoModel2);
					int num2 = this.BllAccountSubject.Add(infoModel2);
					if (num2 > 0)
					{
						this.WriteSuccessMsg("", "会计科目信息添加成功！", this.beforeurl + "&ASParentID=" + this.hfAccountSParentID.Value);
					}
					else
					{
						this.WriteErrMsg("", "会计科目信息添加失败！", "");
					}
				}
			}
		}
	}
	private void GetRootIDOrDepthByParentID()
	{
		DataTable dataTable = this.BllAccountSubject.GetList("AccountSParentID=0 order by AccountSRootID desc").Tables[0];
		if (dataTable.Rows.Count > 0)
		{
			string value = dataTable.Rows[0]["AccountSRootID"].ToString();
			if (!string.IsNullOrEmpty(value))
			{
				this.hfAccountSRootID.Value = (Convert.ToInt32(value) + 1).ToString();
				this.hfAccountSDepth.Value = "0";
				this.txtAccountSNumber.Text = this.hfAccountSRootID.Value + this.hfAccountSDepth.Value + "01";
			}
		}
		else
		{
			this.hfAccountSRootID.Value = "1";
			this.hfAccountSDepth.Value = "0";
			this.txtAccountSNumber.Text = this.hfAccountSRootID.Value + this.hfAccountSDepth.Value + "01";
		}
	}
	private void GetRootIDOrDepthByID(int id)
	{
		HoWave.Data.Model.AccountSubject model = this.BllAccountSubject.GetModel(id);
		if (model != null)
		{
			if (!string.IsNullOrEmpty(model.AccountSDepth.ToString()))
			{
				this.hfAccountSRootID.Value = model.AccountSRootID.ToString();
				this.hfAccountSDepth.Value = (model.AccountSDepth + 1).ToString();
			}
		}
	}
	private void SetRootIDDepth(HoWave.Data.Model.AccountSubject getinfo)
	{
		if (!string.IsNullOrEmpty(this.hfAccountSRootID.Value))
		{
			getinfo.AccountSRootID = new int?(Convert.ToInt32(this.hfAccountSRootID.Value));
		}
		if (!string.IsNullOrEmpty(this.hfAccountSDepth.Value))
		{
			getinfo.AccountSDepth = new int?(Convert.ToInt32(this.hfAccountSDepth.Value));
		}
	}
}
